package com.base.helper;

import com.base.comm.CommUrls;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CallHelper14 {
    private static  String callUrl="http://101.132.253.123:89/api/mobile_apicall.ashx";

    private final static String appid="581798";
    private final static String return_url= CommUrls.THISSERVER;

    private final static String callBackUrl="callBack/call14Back";

    private final static String key="9235C940FF2B714384222703981B9E4D";

    public static String doCall(String caller ,String caller164,String ip,String userData) throws UnsupportedEncodingException {
        String url="appid="+appid+"&callees="+caller164+"&caller="+caller+"&callerIp="+ip+"&return_url="+return_url+callBackUrl;
        String urlParam="appid="+appid+"&callees="+caller164+"&caller="+caller+"&callerIp="+ip+"&return_url="+return_url+callBackUrl+"&userData="+userData;
        String signSource="&"+url;
        String cs=signSource+key;
        String sign=encode(cs);
        String result=WebHelper.GetData(callUrl,urlParam+"&sign="+sign);
        return result;
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        System.out.println(doCall("18055603791","13736686915","192.168.10.33","calltest"));
    }
    /**
     * md5加密
     */
    public static String encode(String text){

        try {
            MessageDigest digest = MessageDigest.getInstance("md5");
            byte[] result = digest.digest(text.getBytes());
            StringBuilder sb =new StringBuilder();
            for(byte b:result){
                int number = b&0xff;
                String hex = Integer.toHexString(number);
                if(hex.length() == 1){
                    sb.append("0"+hex);
                }else{
                    sb.append(hex);
                }
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return "" ;
    }

    /**
     * 参数拼接
     */
    public static String getParam(Map<String, Object> paramsMap) {
        // 返回拼接参数
        String param = "";

        try {
            List<String> params = new ArrayList<>();
            for (String key : paramsMap.keySet()) {
                String values = key + "=" + paramsMap.get(key);
                params.add(values);
            }
            for (int i = 0; i < params.size(); i++) {
                if (i == 0) {
                    param += params.get(i);
                } else {
                    param += "&" + params.get(i);
                }
            }
            // 参数不为空 拼接sign
//            if (param != null && !param.equals("") && sign != null && !sign.equals("")) {
//                param += "&sign=" + sign;
//            }
        } catch (Exception e) {
            System.out.println("API参数拼接失败");
        }

        return param;
    }

//    &appid=3004&callees=18900001111&caller=13800138000&callerIp=127.0.0.1&return_url=http://www.callback.com
}
